<?php
/**
 * Created by PhpStorm.
 * User: krystal
 * Date: 2019/4/15
 * Time: 10:13
 */

namespace app\api\controller\v10;


use app\api\controller\v2\Auth;
use app\common\model\LuckyPlanet as LuckyPlanetModel;

/**
 * 卡片幸运星
 * Class luckyPlanet
 * @package app\api\controller\v10
 */
class luckyPlanet extends Auth
{
    /**
     *  幸运星列表
     * @return \think\response\Json
     */
    public function index()
    {
        $uid = $this->user['id'];
        $page = input('param.page', 1);

        $planet_data = (new LuckyPlanetModel())->getMyLuckyPlanetsPaginate(['uid' => $uid, 'page' => $page]);

        $data = $this->getPlanet($uid);

        $arr ['star_for_num'] = [
            'total_num' => $data['count'],
            'complete_num' => $data['complete'],
        ];
        $arr ['star_for_history'] = [
            'total' => $planet_data['total'],
            'per_page' => $planet_data['per_page'],
            'current_page' => $planet_data['current_page'],
            'last_page' => $planet_data['last_page'],
            'data' => []
        ];
        if ($planet_data['data'])
        {
            foreach ($planet_data['data'] as $k => $p)
            {
                $arr['star_for_history']['data'][$k]['id'] = $p['id'];
                $arr['star_for_history']['data'][$k]['title'] = $p['tip'];
                $arr['star_for_history']['data'][$k]['create_time'] = date('Y-m-d H:i:s', $p['create_time']);
            }
        }

        return show(config('code.error'), '获取成功', $arr);
    }

    /**
     * 中奖幸运星
     * @return \think\response\Json
     */
    public function prizePlanet()
    {
        $uid = $this->user['id'];

        $LuckyPlanet = new LuckyPlanetModel();

        $data = $LuckyPlanet->getUserPrize(['uid' => $uid]);

        $arr = [];
        $arr['star_for_num'] = [
            'prize_num' => count($data),
            'icon' => ''
        ];
        if ($data)
        {
            foreach ($data as $datum)
            {
                $arr['star_for_history'][] = [
                    'id' => $datum['id'],
                    'icon' => '',
                    'title' => $datum['text'],
                    'status' => $datum['status'],
                    'question' => $datum['question'] ?? '',
                    'answer' => $datum['answer'] ?? '',
                    'create_time' => date('Y-m-d', $datum['create_time']),
                ];
            }
        }

        return show(config('code.success'), '加载成功', $arr);
    }

    /**
     * 返回卡片幸运星数据
     * @return \think\response\Json
     */
    public function cardPlanet()
    {
        $uid = $this->user['id'];
        $data = $this->getPlanet($uid);

        return show(config('code.error'), '获取成功', $data['data']);
    }

    /**
     * 获取用户卡片信息并统计抽取数据
     * @return array
     */
    private function getPlanet($uid)
    {

        $LuckyPlanet = new LuckyPlanetModel();

        //获取用户星座类幸运星数据
        $data = $LuckyPlanet->getGroupCardPlanet($uid);

        //获取幸运星相关配置信息
        $lucky_planet = config('luckyplanet');

        $arr = [];
        $array = [];

        //卡片幸运星页面默认数据
        foreach ($lucky_planet['sign_info'] as $k => $sign)
        {
            $arr[$k]['planet_id'] = $k;
            $arr[$k]['name'] = $lucky_planet['sign_info'][$k]['name'];
            $arr[$k]['icon'] = $lucky_planet['sign_info'][$k]['icon'];
            $arr[$k]['title'] = '';
            $arr[$k]['status'] = 0;

            foreach ($lucky_planet['planet_info'][$k] as $v)
            {
                $arr[$k]['data'][] = [
                    'id' => 0,
                    'icon' => $v['icon'],
                    'name' => $v['name'],
                    'num' => 0,
                ];
            }
        }
        //不同星座幸运星抽取数默认为0
        $count = [
            1 => 0,
            2 => 0,
            3 => 0,
            4 => 0,
            5 => 0,
            6 => 0,
            7 => 0,
            8 => 0,
            9 => 0,
            10 => 0,
            11 => 0,
            12 => 0,
        ];
        //默认不可兑换
        $is_exchange = false;
        $total_count = 0;
        $total_complete = 0;
        $planet_num = [];
        //重组卡片幸运星数据
        foreach ($data as $k => $datum)
        {
            //获取幸运星星座相关信息
            $num = (int)(substr($datum['planet_num'], 0, 2));
            //获取幸运星金木水火土相关信息
            $planet = (int)(substr($datum['planet_num'], 3, 1)) - 2;
            $arr[$num]['planet_id'] = $num;
            $arr[$num]['name'] = $lucky_planet['sign_info'][$num]['name'];
            $arr[$num]['icon'] = $lucky_planet['sign_info'][$num]['icon'];
            $arr[$num]['status'] = 0;

            $arr[$num]['data'][$planet] = [
                'img' => $lucky_planet['sign_info'][$num]['img'],
                'icon' => $lucky_planet['planet_info'][$num][$planet]['icon'],
                'name' => $lucky_planet['planet_info'][$num][$planet]['name'],
                'text' => $datum['text'],
                'create_time' => $datum['create_time'],
                'num' => $datum['count'],
            ];

            $count[$num] += $arr[$num]['data'][$planet]['num'];
            $arr[$num]['title'] = '已收集' . $count[$num] . '张';

            //若当前星座幸运星不同行星下都有抽取值,则可以兑换
            if ($arr[$num]['data'][0]['num'] && $arr[$num]['data'][1]['num'] && $arr[$num]['data'][2]['num'] && $arr[$num]['data'][3]['num'] && $arr[$num]['data'][4]['num'])
            {
                $is_exchange = true;
                $planet_num[] = $arr[$num]['data'][$planet]['num'];
            }

            if ($is_exchange)
            {
                $arr[$num]['status'] = 1;
                $arr[$num]['title'] = '已集齐' . min($planet_num) . '套';
            }

            $total_count += $arr[$num]['data'][$planet]['num'];
            if ($planet_num)
            {
                $total_complete += min($planet_num);
            } else
            {
                $total_complete = 0;
            }
        }
        $array['data'] = array_values($arr);
        $array['count'] = $total_count;
        $array['complete'] = $total_complete;

        return $array;
    }

    /**
     * 幸运星提问页详情接口
     */
    public function read()
    {
        $uid = $this->user['id'];
        $id = $this->request->only(['id']);

        $LuckyPlanet = new LuckyPlanetModel();

        $planet = $LuckyPlanet->getDetails([0 => $id]);
        $data = $LuckyPlanet->processList($planet, $uid);

        $arr = [
            'header' => [
                'icon' => '',
                'title' => '',
                'des' => ''
            ],
            'prize' => [
                'content' => $data[0]['text'],
                'create_time' => date('Y-m-d', $data[0]['create_time']),
                'status' => $data[0]['status']
            ],
        ];

        if (isset($data[0]['question']))
        {
            $arr['question'] = [
                'content' => $data[0]['question'],
                'create_time' => $data[0]['question_time']
            ];
        } else
        {
            $arr['question'] = null;
        }
        if (isset($data[0]['answer']))
        {
            $arr['answer'] = [
                'content' => $data[0]['answer'],
                'create_time' => $data[0]['answer_time']
            ];
        } else
        {
            $arr['answer'] = null;
        }

        return show(config('code.error'), '获取成功', $arr);
    }

    /**
     * 幸运星兑换接口
     */
    public function exchangePlanet()
    {
        $uid = $this->user['id'];
        $id = $this->request->only(['planet_id']);

        $data = $this->getPlanet($uid);

        $is_exchange = false;
        foreach ($data['data'] as $datum)
        {
            if ($id['planet_id'] == $datum['planet_id'] && $datum['status'] == 1)
            {
                $is_exchange = true;
            }
        }

        if ($is_exchange === false)
        {
            return show(config('code.error'), '你当前星座幸运星未集齐,不可兑换');
        } else
        {

            return show(config('code.success'), '兑换成功');
        }

    }

}
